package com.yx.pc.api.controller;

import com.yx.common.utils.PageUtils;
import com.yx.common.utils.Query;
import com.yx.common.utils.R;
import com.yx.modules.api.annotation.AuthIgnore;
import com.yx.modules.api.entity.TbInterview;
import com.yx.modules.api.entity.TbInterviewInfo;
import com.yx.pc.api.service.PcTbInterviewInfoService;
import com.yx.pc.api.service.PcTbInterviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * 访谈详情
 * 
 * @author luo
 * @email www.baidu.com
 * @date 2018-06-14 14:59:11
 */
@RestController
@RequestMapping("/pc/api/interviewinfo")
public class PcTbInterviewInfoController {
	@Autowired
	private PcTbInterviewInfoService interviewInfoService;

	@Autowired
	private PcTbInterviewService interviewService;

	/**
	 * 列表
	 */
	@AuthIgnore
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params){
		//取得访谈id
		Integer id = Integer.parseInt(params.get("interviewId").toString());
		if (id==null){
			return R.error("请选择存在的访谈");
		}
		//根据访谈id，查询访谈，增加点击数
		TbInterview tbInterview = interviewService.queryObject(id);
		if (tbInterview==null){
			return R.error("请选择存在的访谈");
		}
		tbInterview.setClickCount(tbInterview.getClickCount()+1);
		interviewService.update(tbInterview);

		//查询列表数据
        Query query = new Query(params);

		List<TbInterviewInfo> interviewInfoList = interviewInfoService.queryList(query);
		int total = interviewInfoService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(interviewInfoList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	public R info(@PathVariable("id") Integer id){
		TbInterviewInfo interviewInfo = interviewInfoService.queryObject(id);
		
		return R.ok().put("interviewInfo", interviewInfo);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public R save(@RequestBody TbInterviewInfo interviewInfo){
		interviewInfoService.save(interviewInfo);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	public R update(@RequestBody TbInterviewInfo interviewInfo){
		interviewInfoService.update(interviewInfo);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public R delete(@RequestBody Integer[] ids){
		interviewInfoService.deleteBatch(ids);
		
		return R.ok();
	}
	
}
